import 'package:flutter/material.dart';
import 'package:flutter_hplus/theme/theme.dart';
import 'h_data_column.dart';

/// 表格头部构建组件
class TableHeader extends StatelessWidget {
  final List<HDataColumn> columns;
  final List<double> columnWidths;
  final Function(int) onSort;
  final Function(int, double) onColumnResize;
  final Function(int) onColumnReset;
  final bool showBorder;
  final int? sortColumnIndex;
  final bool sortAscending;
  final double? height;
  final ScrollController horizontalScrollController;

  const TableHeader({
    super.key,
    required this.columns,
    required this.columnWidths,
    required this.onSort,
    required this.onColumnResize,
    required this.onColumnReset,
    required this.showBorder,
    required this.horizontalScrollController,
    this.sortColumnIndex,
    this.sortAscending = true,
    this.height,
  });
  @override
  Widget build(BuildContext context) {
    final theme = HTheme.of(context);
    
    return Container(
      height: height ?? theme.dimens.inputHeight,
      padding: const EdgeInsets.only(left: 10, right: 10),
      decoration: BoxDecoration(
        color: theme.colors.background,
        border: showBorder ? Border(
          bottom: BorderSide(color: theme.colors.divider),
        ) : null,
      ),
      child: Row(
        children: columns.asMap().entries.map((entry) {
          final index = entry.key;
          final column = entry.value;
          
          return GestureDetector(
            // 双击恢复默认宽度
            onDoubleTap: () => onColumnReset(index),
            child: Stack(
              alignment: Alignment.centerRight,
              children: [
                Container(
                  width: columnWidths[index],
                  height: theme.dimens.inputHeight,
                  padding: const EdgeInsets.symmetric(horizontal: 8),
                  alignment: column.numeric ? Alignment.centerRight : Alignment.centerLeft,
                  decoration: column.showDivider && index < columns.length - 1
                    ? BoxDecoration(
                        border: Border(
                          right: BorderSide(
                            color: theme.colors.divider,
                            width: 1.0,
                          ),
                        ),
                      )
                    : null,
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Expanded(
                        child: Text(
                          column.label,
                          style: theme.textStyles.h4.copyWith(
                            fontWeight: FontWeight.bold,
                          ),
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      if (column.sortable)
                        GestureDetector(
                          onTap: () => onSort(index),
                          child: Icon( 
                            sortColumnIndex == index
                              ? (sortAscending ? Icons.arrow_upward : Icons.arrow_downward)
                              : Icons.unfold_more,
                            size: 20,
                            color: sortColumnIndex == index 
                              ? theme.colors.primary 
                              : theme.colors.textSecondary,
                          ),
                        ),
                    ],
                  ),
                ),
                // 宽度调整控件 - 仅对非最后一列显示
                if (index < columns.length - 1)
                  MouseRegion(
                    cursor: SystemMouseCursors.resizeLeftRight,
                    child: GestureDetector(
                      onHorizontalDragUpdate: (details) {
                        // 调整列宽
                        double newWidth = columnWidths[index] + details.delta.dx;
                        
                        // 应用最小/最大宽度限制
                        if (column.minWidth != null && newWidth < column.minWidth!) {
                          newWidth = column.minWidth!;
                        }
                        if (column.maxWidth != null && newWidth > column.maxWidth!) {
                          newWidth = column.maxWidth!;
                        }
                        
                        // 确保列宽不会变得过小并通知父组件
                        if (newWidth > 40) {
                          onColumnResize(index, newWidth);
                        }
                      },
                      child: Container(
                        width: 8,
                        height: theme.dimens.inputHeight,
                        color: Colors.transparent,
                      ),
                    ),
                  ),
              ],
            ),
          );
        }).toList(),
      ),
    );
  }
} 